package com.yc.sakuralxm.uestcscanning.tools

import android.app.Activity
import android.content.Intent
import android.os.Parcelable

import java.io.Serializable

/**
 * Created by Administrator on 2016/5/7.
 */
object IntentUtil {
    var ALTER_REQUEST = 1
    fun <T : Parcelable> putParcelableExtra(intent: Intent, item: T) {
        intent.putExtra(item.javaClass.simpleName, item)
    }

    fun <T : Serializable> putSerExtra(intent: Intent, item: T) {
        intent.putExtra(item.javaClass.simpleName, item)
    }

    fun <T : Serializable> getSerExtra(intent: Intent?, class1: Class<T>): T? {
        if (intent == null) {
            return null
        }
        val `object` = intent.getSerializableExtra(class1.simpleName) ?: return null
        return `object` as T
    }

    fun <T : Parcelable> getParcelableExtra(intent: Intent?, class1: Class<T>): T? {
        return intent?.getParcelableExtra(class1.simpleName)
    }

    @JvmOverloads
    fun getBooleanExtra(intent: Intent?, deV: Boolean = true): Boolean {
        return intent?.getBooleanExtra(Boolean::class.java.simpleName, deV) ?: deV
    }


    fun startActivity(activity: Activity, class1: Class<out Activity>, `is`: Boolean) {
        val intent = Intent(activity.applicationContext, class1)
        intent.putExtra(Boolean::class.java.simpleName, `is`)
        activity.startActivity(intent)
    }

    fun startActivity(activity: Activity, class1: Class<out Activity>, vararg ss: Any) {
        val intent = Intent(activity.applicationContext, class1)
        for (s in ss) {
            if (s is Parcelable) {
                putParcelableExtra(intent, s)
            } else if (s is Serializable) {
                putSerExtra(intent, s)
            }
        }
        activity.startActivity(intent)
    }

    fun startActivityForResult(activity: Activity, class1: Class<out Activity>, vararg ss: Serializable) {
        val intent = Intent(activity.applicationContext, class1)
        for (s in ss) {
            putSerExtra(intent, s)
        }
        activity.startActivityForResult(intent, ALTER_REQUEST)
    }

    fun finishForResult(activity: Activity, vararg values: Parcelable) {
        L.i("finishForResult start")
        val intent = Intent()
        if (values != null)
            for (p in values) {
                if (p != null) {
                    L.i("finishForResult start " + p.javaClass.simpleName)
                    intent.putExtra(p.javaClass.simpleName, p)
                }
            }
        L.i("finishForResult end")
        activity.setResult(Activity.RESULT_OK, intent)
        activity.finish()
    }

    fun finishForResult(activity: Activity) {
        val intent = Intent()
        intent.putExtra(Boolean::class.java.simpleName, true)
        activity.setResult(Activity.RESULT_OK, intent)
        activity.finish()
    }

    fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
        return Activity.RESULT_OK == resultCode && requestCode == ALTER_REQUEST && data != null && data.getBooleanExtra(Boolean::class.java.simpleName, false)
    }

}
